<?php
namespace Home\Controller;

class GetpwdController extends HomeController {

    public function index(){
        $this->display();
    }

    public function send(){
        $username = I('post.username');
        $map['username'] = $username;
        $list = M('Member')->where($map)->find();
        if ($list) {
            $name = '';
            $title = C('site_name').' Member password back';
            $verify = md5('getpwd'.$username.time());
            $this->assign('verify', $verify);

            $content = $this->fetch('email');
            //think_send_mail($username, $name, $title, $content); 

            $url = 'http://api.kf01.com/email/mentoring.php';
            $data = array(
                    'email' => $username,
                    'username' => $name,
                    'title' => $title,
                    'content' => $content,
                );

            $this->https_post($url,$data);

            M('Member')->where($map)->setField('verify',$verify);

            $state = 'Email has been sent to your mailbox, please login to the mailbox to reset the password';
        }else{
            $state = 'Email error';
        }
        echo $this->echo_json_str($state,$message,$url); 
    }

    public function reset(){
        $code = I('get.code');
        if ($code=='') {
            $this->error('Parameter error');
        }
        $this->assign('code',$code);
        $this->display();
    }

    public function save(){
        $code = I('post.code');
        $password = I('post.password');
        if ($code=='') {
            $this->error('Parameter error');
        }
        if ($password=='') {
            $this->error('Password can not be empty');
        }
        $map['verify'] = $code;
        $list = M('Member')->where($map)->find();
        if (!$list) {
            $this->error('Link has failed');
        }else{
            $data['password'] = md5($password);
            $data['verify'] = '';
            M('Member')->where($map)->save($data);
            $url = U('Login/index');
            redirect($url);
            //$this->success('Password set successfully',$url);
        }
    }
}